package com.easyboot.web.monitor.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easyboot.framework.domain.DataTableDto;
import com.easyboot.web.monitor.domain.MonitorJobLog;
import com.easyboot.web.monitor.mapper.MonitorJobLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *任务执行日志
 */
@Service
public class MonitorJobLogService extends ServiceImpl<MonitorJobLogMapper, MonitorJobLog>{

    @Autowired
    private MonitorJobLogMapper jobLogMapper;


    /**
    *
     * @author yushuo
     * @description 添加执行日志
     * @date 14:19 2021/6/24
     * @param [log]
     * @return void
    **/
    public void addJobLog(MonitorJobLog log){

        jobLogMapper.insert(log);
    }


    /**
    *
     * @author yushuo
     * @description 获取数据列表
     * @date 15:20 2021/6/24
     * @param [dto, jobId]
     * @return com.easyboot.framework.domain.DataTableDto
    **/
    public DataTableDto jobLogData(DataTableDto dto,long jobId){
        Date nowTime = new Date();
        Date startTime = DateUtil.offsetDay(nowTime, -7);
        LambdaQueryWrapper<MonitorJobLog> query=new LambdaQueryWrapper<>();
        query.eq(MonitorJobLog::getJobId,jobId);
        query.ge(MonitorJobLog::getExcuteTime,startTime);
        query.orderByDesc(MonitorJobLog::getExcuteTime);
        Page<MonitorJobLog> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        IPage<MonitorJobLog> userIPage = jobLogMapper.selectPage(page, query);
        dto.setTotal(userIPage.getTotal());
        dto.setRows(userIPage.getRecords());
        return dto;
    }


    /**
     * @author yushuo
     * @description 删除全部log
     * @date 15:25 2021/6/24
     * @param [jobId]
     * @return void
    **/
    public void clearJobLog(long jobId){
        LambdaUpdateWrapper<MonitorJobLog> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(MonitorJobLog::getJobId,jobId);
        jobLogMapper.delete(updateWrapper);

    }
}




